package com.choosefine.it.project.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.exception.BusinessException;
import com.choosefine.it.project.model.ProjectWorkerInfo;
import com.choosefine.it.project.model.WorkerAttendanceDetail;
import com.choosefine.it.project.model.po.ProjectWorkerCondPO;
import com.choosefine.it.project.model.po.WorkerAttendancePO;
import com.choosefine.it.project.service.ProjectWorkerService;
import com.choosefine.it.worker.service.WorkerSalaryDayInterfaceService;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 控制台-项目管理-工地数据管理-工人出勤 入口
 * 
 * @auth liaozhanggen 2016-12-10 下午8:14:02
 *
 */
@RestController
@RequestMapping("/projectmgt/console")
@Api(value = "控制台-项目管理-工地数据管理-工人出勤", description = "廖仉根")
public class ProjectWorkerController extends BaseController {

	@Autowired
	private ProjectWorkerService projectWorkerService;
	@Autowired
	private WorkerSalaryDayInterfaceService workerSalaryDayInterfaceService;

	@ResponseBody
	@ApiOperation("工人出勤-员工成员列表（基本信息、班组信息）")
	@RequestMapping(value = "/query/worker/{projectCode}", method = RequestMethod.POST)
	public String queryProjectWorkerInfos(@RequestBody ProjectWorkerCondPO workerCondPO,
			@PathVariable(value = "projectCode", required = true) String projectCode) throws BusinessException {

		workerCondPO.setProjectCode(projectCode);
		Page<ProjectWorkerInfo> workerInfos = projectWorkerService.queryProjectWorkerInfos(workerCondPO);
		return responseSuccess(workerInfos.toPageInfo());
	}

	@ResponseBody
	@ApiOperation("工人出勤-员工成员列表-某工人出勤明细列表")
	@RequestMapping(value = "/attendance/detail", method = RequestMethod.POST)
	public String queryWorkerAttenDetailInfos(@RequestBody WorkerAttendancePO workerAttenPO) throws BusinessException {

		Page<WorkerAttendanceDetail> wads = projectWorkerService.queryWorkerAttendDetailByUserCode(workerAttenPO);
		return responseSuccess(wads.toPageInfo());
	}

	@ResponseBody
	@ApiOperation("工人出勤-员工成员列表-某工人出勤明细列表-删除功能")
	@RequestMapping(value = "/attendance/invalid", method = RequestMethod.GET)
	public String modifyWorkerAttendanceInvalid(
			@RequestParam(value = "workerAttendanceId", required = true) String workerAttendanceIds)
			throws BusinessException {
		int num = workerSalaryDayInterfaceService.modifyWorkerAttendInvalid(workerAttendanceIds);
		return num > 0 ? responseSuccess() : responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Current Record"));
	}

	@ResponseBody
	@ApiOperation("工人出勤-员工成员列表-某工人出勤明细列表-可批量/单个添加考勤-功能")
	@RequestMapping(value = "/attendance/add", method = RequestMethod.POST)
	public String addBatchWorkerAttendance(@RequestBody List<WorkerAttendanceDetail> record) throws Exception {
		for (WorkerAttendanceDetail wsdi : record) {
			if (StringUtils.isEmpty(wsdi.getProjectCode())) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Project Code"));
			}
			if (StringUtils.isEmpty(wsdi.getUserCode())) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "User Code"));
			}
			if (null == wsdi.getTeamId() || 0 == wsdi.getTeamId().intValue()) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Team Id"));
			}
			if (StringUtils.isEmpty(wsdi.getDataTime())) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "The Data Time"));
			}
			if (StringUtils.isEmpty(wsdi.getOrgWorkTimeStart())) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "The start time"));
			}
			if (StringUtils.isEmpty(wsdi.getOrgWorkTimeEnd())) {
				return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "The end time"));
			}
		}

		int num = workerSalaryDayInterfaceService.insertWorkerAttendanceRecord(record);
		return num > 0 ? responseSuccess() : responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Data Time"));
	}

	@ResponseBody
	@ApiOperation("工人出勤-员工成员列表-某工人出勤明细列表-可批量/单个编辑修改考勤-功能")
	@RequestMapping(value = "/attendance/edit", method = RequestMethod.POST)
	public String editBatchWorkerAttendance(@RequestBody List<WorkerAttendanceDetail> record) throws Exception {
		for (WorkerAttendanceDetail wsdi : record) {
			if (null == wsdi.getId()) { // 判断主键是否为空，如果为空，则判断当前的职员所在的项目及班组中是否为空
				if (StringUtils.isEmpty(wsdi.getProjectCode())) {
					return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Project Code"));
				}
				if (StringUtils.isEmpty(wsdi.getUserCode())) {
					return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "User Code"));
				}
				if (null == wsdi.getTeamId() || 0 == wsdi.getTeamId().intValue()) {
					return responseFail(String.format(CommonConstant.COMMON_IS_VALID, "Team Id"));
				}
			}
		}

		int num = workerSalaryDayInterfaceService.editWorkerAttendanceRecord(record);
		return num > 0 ? responseSuccess() : responseFail();
	}

}